//
//  GJTextField.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "UITextField+Cashier.h"
#import "UITextField+Style.h"
#import <objc/runtime.h>

static const void *kRightTextKey = @"kRightTextKey";

@implementation GJButton_

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    if (!_titleLab) {
        _titleLab = [UILabel labelWithFrame:CGRectMake(0, 0, [@" 8.8折" widthWithFont:FontCustomSize(16) constrainedToHeight:16], 48) text:nil fontSize:16 textColor:UIColorFromRGB(0xff4f4f) textAlignment:NSTextAlignmentRight];
        [self addSubview:_titleLab];
    }
    
    [self addTarget:self action:@selector(chooseDiscountAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if (self.titleLab.text.length==0) {
        return;
    }
    CGFloat textWidth = ([@" 8.8折" widthWithFont:FontCustomSize(16) constrainedToHeight:16]);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, textWidth+4, 32);
    CGContextAddLineToPoint(context, textWidth+4+8, 24);
    CGContextAddLineToPoint(context, textWidth+4+8, 32);
    CGContextSetFillColorWithColor(context, UIColorFromRGB(0xa4a4a4).CGColor);
    CGContextFillPath(context);
}

- (void)chooseDiscountAction:(id)btn
{
#pragma clang diagnostic push //忽略警告
#pragma clang diagnostic ignored "-Wundeclared-selector"
    [self.delegate performSelector:@selector(chooseDiscountAction)];
#pragma clang diagnostic pop
}

@end

@implementation UITextField (Cashier)

- (void)setRightText:(NSString *)rightText
{
    objc_setAssociatedObject(self, kRightTextKey, rightText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    GJButton_ *btn = ((GJButton_ *)self.rightView);
    btn.titleLab.text = rightText;
    [btn setNeedsDisplay];
}

- (NSString *)rightText
{
    return objc_getAssociatedObject(self, kRightTextKey);
}

- (void)setRightButton
{
    [self clearRightButton];
    if ([self.leftText rangeOfString:@"手机号"].location != NSNotFound) {
        GJButton_ *rightBtn = [GJButton_ buttonWithType:UIButtonTypeCustom];
        rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        rightBtn.delegate = self;
        CGFloat btnWidth = [@" 8.8折" widthWithFont:FontCustomSize(16) constrainedToHeight:16]+8+4;
        rightBtn.frame = CGRectMake(0, 0, btnWidth, self.height);
        self.rightView = rightBtn;
        self.rightViewMode = UITextFieldViewModeAlways;
    }
}

- (void)clearRightButton
{
    [self.rightView removeFromSuperview];
    self.rightView = nil;
}

- (void)chooseDiscountAction
{
#pragma clang diagnostic push //忽略警告
#pragma clang diagnostic ignored "-Wundeclared-selector"
    [[self viewController] performSelector:@selector(showDiscountActionSheet)];
#pragma clang diagnostic pop
}

- (id)viewController
{
    UIResponder *rsp = self;
    while (![rsp isKindOfClass:[UIViewController class]]) {
        rsp = rsp.nextResponder;
    }
    return rsp;
}

@end
